<?php
class Page {
	// 当前页数
    protected $nowPage;
	// 总行数
    protected $totalRows;
	// 总页数
    protected $totalPages;
	// 列表每页显示行数
    public $listRows;
	
	public function __construct($totalRows,$listRows,$parameter='') {
		$this->totalRows = $totalRows;
        $this->parameter = $parameter;
		$this->listRows = !empty($listRows)?$listRows:C('PAGE_LISTROWS');
		$this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
		$this->nowPage  = !empty($_GET['page'])?$_GET['page']:1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
	}
	
	public function show() {
        if(0 == $this->totalRows) return '无记录分页';
        $p = 'page';       
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$p]);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
        //上下翻页字符串
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
		
		$stcShow = '<b>'.$this->totalRows.'条记录</b> '.$this->nowPage.'/'.$this->totalPages.'页 ';
		if($this->nowPage > 1){
			$stcShow .= "<a href='".$url.'&'.$p."=1'>第一页</a> ";
			$stcShow .= "<a href='".$url.'&'.$p."=".$upRow."'>上一页</a> ";
		}		
		if($this->nowPage < $this->totalPages){
			$stcShow .= "<a href='".$url.'&'.$p."=".$downRow."'>下一页</a> ";
			$stcShow .= "<a href='".$url.'&'.$p."=".$this->totalPages."'>最后一页</a> ";
		}
		$startRow = ($this->nowPage-4<1) ? 1 : ($this->nowPage-4);
		$endRow   = ($this->nowPage+5>$this->totalPages) ? $this->totalPages : ($this->nowPage+5);
		for($i=$startRow; $i<=$endRow; $i++){
			if($i==$this->nowPage){
				$stcShow .= "<a class='curr'>[".$i."]</a> ";
			}else{
				$stcShow .= "<a href='".$url.'&'.$p."=".$i."'>[".$i."]</a> ";
			}
		}
		
		return $stcShow;
	}
}
?>